package cn.bigfire.wzdj.frame;


import cn.bigfire.wzdj.util.ImgCmp;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class QrCanvas extends Canvas {

    private volatile int opCode = 0;
    private BufferedImage img = null;
    private String text = "";

    public void drawImg(BufferedImage img) {
        opCode = 1;
        this.img = ImgCmp.resizeImage(img, 100, 100);
        this.repaint();
    }

    public void drawText(String text) {
        opCode = 2;
        this.text = text;
        this.repaint();
    }


    public void drawInit(BufferedImage img, String text) {
        opCode = 3;
        this.img = img;
        this.text = text;
        this.repaint();
    }

    @Override
    public void paint(Graphics graphics) {
        if (opCode == 1) {
            if (this.img != null) {
                graphics.drawImage(this.img, 0, 0, null);
            }
        } else if (opCode == 2) {
            if (text != null && !text.isEmpty()) {
                graphics.drawString(this.text, 0, 20);
            }
        }else if (opCode == 3) {
            if (text != null && !text.isEmpty() && this.img != null) {
                graphics.drawImage(this.img, 15, 0, null);
                graphics.drawString(this.text, 7, 95);
            }
        }
    }

}
